home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Display Manager SDK / Sample Code / Display Changed CWPro4 / Source / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-20  |  4.9 KB  |  217 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        utils.c
  4. #
  5. #        This segment handles file parsing, and basic utility functions.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            1/19/99        ewa     update to CWPro3 and universal interfaces                     
  13. #            10/12/95    MWM        cleaned up
  14. #            6/4/95        MWM     Initial coding                     
  15. #
  16. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. #        You may incorporate this sample code into your applications without
  20. #        restriction, though the sample code has been provided "AS IS" and the
  21. #        responsibility for its operation is 100% yours.  However, what you are
  22. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  23. #        after having made changes. If you're going to re-distribute the source,
  24. #        we require that you make it clear in the source that the code was
  25. #        descended from Apple Sample Code, but that you've made changes.
  26. #
  27. *************************************************************************************/
  28.  
  29. #include <Events.h>
  30. #include <ToolUtils.h>
  31. #include <Gestalt.h>
  32. #include <OSUtils.h>
  33. #include <StandardFile.h>
  34.  
  35. #include "App.h"
  36. #include "Proto.h"
  37.  
  38.  
  39. //----------------------------------------------------------------------
  40. //
  41. //    DoOpenNew - query user for new file of type 'PICT'. Open it and 
  42. //                display in a new window.
  43. //
  44. //----------------------------------------------------------------------
  45.  
  46. void DoOpenNew(void)
  47. {
  48.     OSErr                    err;
  49.     PicHandle                pict;
  50.     StandardFileReply        reply;
  51.     SFTypeList                 typeList;
  52.     OSType                    type = 'PICT';
  53.     WindowRef                window;
  54.     Rect                    bounds;
  55.     Rect                    screenRect;
  56.     short                    numTypes;
  57.     DocHnd                    doc;
  58.  
  59.     numTypes = 1;
  60.     typeList[0] = type;
  61.     StandardGetFile(nil, numTypes, (unsigned long *)&typeList, &reply);
  62.     if ( reply.sfGood )
  63.     {
  64.         pict = ReadFile(reply.sfFile);
  65.         if (pict != nil) 
  66.         {
  67.             bounds = (**pict).picFrame;
  68.             ZeroRect(&bounds);
  69.             OffsetRect(&bounds, 2, GetMBarHeight() * 2);
  70.             bounds.right += kScrollWidth;
  71.             bounds.bottom += kScrollWidth;
  72.             screenRect = qd.screenBits.bounds;
  73.             if (bounds.right > screenRect.right) 
  74.                 bounds.right = screenRect.right;
  75.             if (bounds.bottom > screenRect.bottom) 
  76.                 bounds.bottom = screenRect.bottom;
  77.  
  78.             window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
  79.                                   zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
  80.  
  81.             if (window != nil) 
  82.             {
  83.                 doc = (DocHnd)GetWRefCon(window);
  84.                 if (doc != nil)
  85.                 {
  86.                     HLock((Handle)doc);
  87.                     (**doc).world = PictToWorld(pict, &err);
  88.                     if ((**doc).world == nil || err != noErr) 
  89.                     {
  90.                         KillPicture(pict);
  91.                         RemoveWindow(window);
  92.                         HandleError(err, false);
  93.                     }
  94.                     else
  95.                         AdjustScrollbars(window,true);
  96.                     
  97.                     HUnlock((Handle)doc);
  98.                     
  99.                 }
  100.                 
  101.             }
  102.             if (pict != nil)
  103.                 KillPicture(pict);
  104.         }                              
  105.     }
  106. }
  107.  
  108.  
  109. //----------------------------------------------------------------------
  110. //
  111. //    ReadFile - open and read disk data, returning PICT.
  112. //                
  113. //
  114. //----------------------------------------------------------------------
  115.  
  116. PicHandle ReadFile(FSSpec spec)
  117. {
  118.     OSErr                    err;
  119.     PicHandle                pict;
  120.     long                    pictSize;
  121.     long                    fileSize;
  122.     short                    refNum;
  123.  
  124.     SetCursor(*GetCursor(watchCursor));            // set the cursor to a watch while busy
  125.     
  126.     err = FSpOpenDF(&spec, fsRdWrShPerm, &refNum);
  127.     if ( err == noErr) 
  128.     {
  129.         err = GetEOF(refNum, &fileSize);
  130.         if (err == noErr ) 
  131.         {
  132.             err = SetFPos(refNum, fsFromMark, 512);   // set position to our picture data
  133.             if (err == noErr) 
  134.             {
  135.                 pictSize = fileSize - 512;
  136.                 pict = (PicHandle)NewHandle(pictSize);
  137.                 err = MemError();
  138.  
  139.                 if (err == noErr && pict != nil) 
  140.                 {
  141.                     HLock((Handle) pict);
  142.                     err = FSRead(refNum, &pictSize, *pict);        // read in the pict data
  143.                     HUnlock((Handle) pict);
  144.                 }
  145.             }
  146.         }
  147.     
  148.         FSClose(refNum);            // close the file
  149.         SetCursor(&qd.arrow);        // set cursor back to arrow
  150.     }
  151.     
  152.     if (err != noErr)
  153.         pict = nil;
  154.     
  155.     return pict;
  156.  
  157. }
  158.     
  159.  
  160. //----------------------------------------------------------------------
  161. //
  162. //    ZeroRect - zero the top-left points of a rect
  163. //                
  164. //
  165. //----------------------------------------------------------------------
  166.  
  167. void ZeroRect(Rect *r)
  168. {
  169.     Rect        zr;
  170.     
  171.     zr = *r;
  172.     
  173.     if (zr.left < 0)
  174.         OffsetRect(&zr,zr.left,0);
  175.     if (zr.top < 0)
  176.         OffsetRect(&zr,0,zr.top);
  177.     
  178.     OffsetRect(&zr,-zr.left,-zr.top);
  179.     
  180.     *r = zr;
  181.  
  182.  
  183.  
  184. //----------------------------------------------------------------------
  185. //
  186. //    pstrcpy - pascal string copy
  187. //                
  188. //
  189. //----------------------------------------------------------------------
  190.  
  191. void pstrcpy(StringPtr dst, StringPtr src)
  192. {
  193.     short    c;
  194.     
  195.     for (c = *src; c > -1; c--)
  196.         dst[c] = src[c];
  197. }
  198.  
  199.  
  200. //----------------------------------------------------------------------
  201. //
  202. //    pstrcat - pascal string concat
  203. //                
  204. //
  205. //----------------------------------------------------------------------
  206.  
  207. void pstrcat(StringPtr dst, StringPtr src)
  208. {
  209.     
  210.     short    c;
  211.  
  212.     for (c = 1; c < src[0] + 1; c++)
  213.         dst[dst[0] + c] = src[c];
  214.     dst[0] += src[0];
  215. }
  216.